package com.computer.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.computer.cart.Cart;
import com.computer.cart.CartItem;



@WebServlet("/showcomputer")
public class ShowComputerServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		Object obj = request.getSession().getAttribute("cart");
		out.print("<html><body><table border='1'>");
		if(obj != null) {
			Cart cart = (Cart)obj;
			Map<Integer, CartItem> map = cart.getAll();
			map.values().forEach(ci -> {
				out.print("<tr>");
				out.print("<td>" + ci.getP().getBrand() + "</td>");
				out.print("<td>" + ci.getP().getColor() + "</td>");
				out.print("<td>" + ci.getP().getPrice() + "</td>");
				out.print("<td>" + ci.getCount() + "</td>");
				out.print("</tr>");
			});
		}else {
			out.print("您没有购买任何物品");
		}
		out.print("</table></body></html>");
		out.flush();
		out.close();
	
		
		
	}
	
}
